﻿public class Solution {
    public int EvalRPN(string[] tokens)
    {
        Stack<int> stack = new Stack<int>();
        int number1 = 0, number2 = 0;
        foreach (string s in tokens)
        {
            if (int.TryParse(s, out number1)) // 是数字，进栈
            {
                stack.Push(number1);
            }
            else // 是运算符
            {
                number2 = stack.Pop();
                number1 = stack.Pop();
                switch (s)
                {
                    case "+":
                        stack.Push(number1 + number2);
                        break;
                    case "-":
                        stack.Push(number1 - number2);
                        break;
                    case "*":
                        stack.Push(number1 * number2);
                        break;
                    case "/":
                        stack.Push(number1 / number2);
                        break;
                }
            }
        }
        return stack.Pop();
    }

    public static void Main(string[] args)
    {
        string[] tokens = ["2", "1", "+", "3", "*"];
        Solution solution = new Solution();
        int res = solution.EvalRPN(tokens);
        Console.WriteLine(res);
    }
}